package com.zero.zutils
{
	import flash.errors.IllegalOperationError;
	import flash.system.ApplicationDomain;
	import flash.utils.getQualifiedSuperclassName;

	public final class ClassUtils
	{
		private static var _appDomain:ApplicationDomain;
		public static const COLOR_MATIX_FILTER:String = "flash.filters.ColorMatrixFilter";
		
		/**
		 * 
		 * @param className : 类名
		 * @param args : 类的构造函数所需的参数
		 * @return the 该类的实例
		 * 
		 */		
		public static function CreatInstance(className:String,args:Array = null):*
		{
			var instanceClass:Object = _appDomain.getDefinition(className);
			if(instanceClass == null)
			{
				throw new IllegalOperationError("can't fi1nd the class of\""+className+"\"in current domain");
			}
			var instance:Object;
			if(args == null || args.length == 0)
			{
				instance = new instanceClass();
			}
			else
			{
				switch(args.length)
				{
					case 1:
						instance = new instanceClass(args[0]);
						break;
					case 2:
						instance = new instanceClass(args[0],args[1]);
						break;
					case 3:
						instance = new instanceClass(args[0],args[1],args[2]);
						break;
					case 4:
						instance = new instanceClass(args[0],args[1],args[2],args[3]);
						break;
					case 5:
						instance = new instanceClass(args[0],args[1],args[2],args[3],args[4]);
						break;
					case 6:
						instance = new instanceClass(args[0],args[1],args[2],args[3],args[4],args[5]);
						break;
					case 7:
						instance = new instanceClass(args[0],args[1],args[2],args[3],args[4],args[5],args[6]);
						break;
					case 8:
						instance = new instanceClass(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7]);
						break;
					case 9:
						instance = new instanceClass(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8]);
						break;
					case 10:
						instance = new instanceClass(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8],args[9]);
						break;
					case 11:
						instance = new instanceClass(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8],args[9],args[10]);
						break;
					case 12:
						instance = new instanceClass(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8],args[9],args[10],args[11]);
						break;
					case 13:
						instance = new instanceClass(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8],args[9],args[10],args[11],args[12]);
						break;
					default:
						throw new IllegalOperationError("arguments too long");
						break;
				}
			}
			
			instanceClass = null;
			return instance;
		}
		/**
		 * 
		 * 用于取得Component类的域
		 * 
		 */		
		public static function get uiSourceDomain():ApplicationDomain
		{
			return _appDomain;
		}
		
		public static function set uiSourceDomain(domain:ApplicationDomain):void
		{
			_appDomain = domain;
		}
		
		public static function getDefinition(classname:String):Object
		{
			return _appDomain.getDefinition(classname);
		}
	}
}